package edu.ustb.controller.xiumi;

import com.alibaba.fastjson.JSONObject;
import edu.ustb.service.xiumi.xiumiInputControllerService;
import edu.ustb.service.xiumi.impl.xiumiInputControllerServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "xiumiInputController", value = "/xiumiInput.do")
public class xiumiInputController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        String info = request.getParameter("info");
        String passOld = request.getParameter("passOld");
        String passNew1 = request.getParameter("passNew1");

        JSONObject json = new JSONObject();
        xiumiInputControllerService util =
                new xiumiInputControllerServiceImpl();

        // 验证原始密码是否正确
        if(util.ifChangedPass(info, passOld, passNew1)){
            json.put("status", "true");
            json.put("message", "修改密码成功，请重新登录");
            response.getWriter().println(json);

        }else{
            json.put("status", "false");
            json.put("message", "初始密码错误请确认后输入");
            response.getWriter().println(json);
        }
    }
}
